package com.csw.shuanfa.CodeImprove.poi;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PoiTest {
    public static void main(String[] args) throws IOException {
        String[] titles = {"名字", "年龄"};
        List<Map<String, Object>> mapList = new ArrayList<>();


        int totalCount = (int) Math.pow(10, 6);
        for (int i = 0; i < totalCount; i++) {
            UserPoi userPoi = new UserPoi();
            Map<String, Object> map = new HashMap<>(16);
            userPoi.setName("张三" + i);
            userPoi.setAge(i);
            map.put(i + "", userPoi);
            mapList.add(map);
        }

        XSSFWorkbook workbook = new XSSFWorkbook();

        XSSFSheet sheet = workbook.createSheet("sheet" + 0);

        XSSFRow sxssfRow = sheet.createRow(0);
        for (int a = 0; a < titles.length; a++) {
            sxssfRow.createCell(a).setCellValue(titles[a]);
        }
        for (int a = 1; a <= mapList.size(); a++) {
            sxssfRow = sheet.createRow(a);
            Map<String, Object> objectMap = mapList.get(a - 1);

            for (String str : objectMap.keySet()) {
                UserPoi userPoi = (UserPoi) objectMap.get(str);
                sxssfRow.createCell(0).setCellValue(userPoi.getName().toString());
                sxssfRow.createCell(1).setCellValue(userPoi.getAge());
            }
            //map数据清理
            objectMap.clear();
        }
        //数据清理
        mapList.clear();
        //设置空引用
        mapList = null;


        FileOutputStream fos = new FileOutputStream("D:\\text.xlsx");
        workbook.write(fos);
        fos.close();
        workbook.close();
    }
}
